using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;


namespace MessageBoardControl
{
	/// <summary>
	/// Summary description for GroupList.
	/// </summary>
	[DefaultProperty("Text"), 
		ToolboxData("<{0}:GroupList runat=server></{0}:GroupList>")]
	public class GroupList : System.Web.UI.WebControls.WebControl
	{
		#region Properties
		private string text;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string Text 
		{
			get
			{
				return text;
			}

			set
			{
				text = value;
			}
		}

		private string url;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URL 
		{
			get
			{
				return url;
			}

			set
			{
				url = value;
			}
		}

		private string addUrl;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string AddURL 
		{
			get
			{
				return addUrl;
			}

			set
			{
				addUrl = value;
			}
		}

		private string editUrl;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string EditURL 
		{
			get
			{
				return editUrl;
			}

			set
			{
				editUrl = value;
			}
		}

		private string deleteUrl;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string DeleteURL 
		{
			get
			{
				return deleteUrl;
			}

			set
			{
				deleteUrl = value;
			}
		}

		private string messageBoardSecurityUrl;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string MessageBoardSecurityUrl 
		{
			get
			{
				return messageBoardSecurityUrl;
			}

			set
			{
				messageBoardSecurityUrl = value;
			}
		}

		private string groupSecurityUrl;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string GroupSecurityUrl 
		{
			get
			{
				return groupSecurityUrl;
			}

			set
			{
				groupSecurityUrl = value;
			}
		}

		private MessageBoard.User currentUser;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.User CurrentUser 
		{
			get
			{
				return currentUser;
			}

			set
			{
				currentUser = value;
			}
		}

		private string imagesFolder;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string ImagesFolder 
		{
			get
			{
				return imagesFolder;
			}

			set
			{
				imagesFolder = value;
			}
		}

		private MessageBoard.Language language;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Language Language 
		{
			get
			{
				return language;
			}

			set
			{
				language = value;
			}
		}
		
		#endregion
		
		#region Translation
		// define the control to be translated
		private MessageBoard.CustomControl customControl = MessageBoard.CustomControl.Get("GroupList");
		
		/// <summary>
		/// Translate the words.
		/// </summary>
		/// <param name="word"></param>
		/// <returns></returns>
		private string Translate(string word)
		{
			return MessageBoard.Translation.Get(customControl, word, Language).Equal;
		}
		#endregion

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			CurrentUser.MakeRequest();
			output.WriteBeginTag("table");
			output.WriteAttribute("width", "100%");
			output.WriteAttribute("cellpadding", "0");
			output.WriteAttribute("cellspacing", "0");
			output.RenderBeginTag(HtmlTextWriterTag.Tr);
			output.WriteBeginTag("td");
			output.WriteAttribute("class", "Forum_TableBorder");
			output.Write(HtmlTextWriter.TagRightChar);

			output.WriteBeginTag("table");
			output.WriteAttribute("width", "100%");
			output.WriteAttribute("cellspacing", "1");
			output.WriteAttribute("cellpadding", "2");
				output.RenderBeginTag(HtmlTextWriterTag.Tr);
					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("colspan", "2");
					output.Write(HtmlTextWriter.TagRightChar);
							output.Write(Translate("Groups"));
					output.WriteEndTag("td");

					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("width", "50px");
					output.Write(HtmlTextWriter.TagRightChar);
						if(MessageBoard.Role.Check(currentUser, MessageBoard.Role.Permission.CanManageMessageBoardSecurity) == MessageBoard.Role.Allow.Yes)
						{
							output.Write("<a href='"+messageBoardSecurityUrl+"Forum=ManageSecurity'><img src="+imagesFolder+"/Permissions.gif border=0 title='"+Translate("Message Board Security")+"'></a>");
						}
						if(MessageBoard.Role.Check(currentUser, MessageBoard.Role.Permission.CanAddGroups) == MessageBoard.Role.Allow.Yes)
						{
							output.Write("<a href='"+addUrl+"Forum=AddGroup'><img src="+imagesFolder+"/group_new.gif border=0 title='"+Translate("Add new Group")+"'></a>");
						}
					output.WriteEndTag("td");

				output.RenderEndTag();
				int i=0;

			foreach(MessageBoard.Group group in MessageBoard.Group.GetAll())
			{
				i++;
				if(MessageBoard.Role.Check(currentUser, group, MessageBoard.Role.Permission.CanViewGroups) == MessageBoard.Role.Allow.Yes)
				{
					output.RenderBeginTag(HtmlTextWriterTag.Tr);

					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title3");
					output.WriteAttribute("width", "20");
					output.WriteAttribute("align", "center");
					output.Write(HtmlTextWriter.TagRightChar);
					output.Write("<img src="+imagesFolder+"/closed.gif border=0>");							
					output.WriteEndTag("td");

					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title4");
					output.Write(HtmlTextWriter.TagRightChar);
					output.WriteBeginTag("a");
					output.WriteAttribute("href", URL+"Forum=CategoryList&GroupId="+group.Id);
					output.WriteAttribute("class", "Forum_BoldLink");
					output.Write(HtmlTextWriter.TagRightChar);
					output.Write(group.Title);
					output.WriteEndTag("a");
					output.Write("<br>");
					output.Write(group.Description);
					output.WriteEndTag("td");

					output.WriteBeginTag("td");
					output.WriteAttribute("width", "40");
					output.WriteAttribute("class", "Forum_Title3");
					output.Write(HtmlTextWriter.TagRightChar);
						if(MessageBoard.Role.Check(currentUser, group, MessageBoard.Role.Permission.CanManageGroupsSecurity) == MessageBoard.Role.Allow.Yes)
						{
							output.Write("<a href='"+groupSecurityUrl+"Forum=ManageSecurity&GroupId=" + group.Id + "'><img src="+imagesFolder+"/Permissions.gif border=0 title='"+Translate("Message Group Security")+"'></a>");
						}
						if(MessageBoard.Role.Check(currentUser, group, MessageBoard.Role.Permission.CanEditGroups) == MessageBoard.Role.Allow.Yes)
						{
							output.Write("<a href='"+editUrl+"Forum=EditGroup&GroupId="+group.Id+"'><img src="+imagesFolder+"/group_edit.gif border=0 title='"+Translate("Edit this group")+"'></a> ");
						}
						if(MessageBoard.Role.Check(currentUser, group, MessageBoard.Role.Permission.CanDeleteGroups) == MessageBoard.Role.Allow.Yes)
						{
							output.Write("<a href='"+deleteUrl+"Forum=DeleteGroup&GroupId="+group.Id+"'><img src="+imagesFolder+"/group_delete.gif border=0 title='"+Translate("Delete this group")+"'></a> ");
						}
					output.WriteEndTag("td");

					output.RenderEndTag();
				}
			}
			if(i==0)
			{
				output.RenderBeginTag(HtmlTextWriterTag.Tr);
					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title3");
					output.WriteAttribute("colspan", "3");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write("No groups found.");
					output.WriteEndTag("td");
				output.RenderEndTag();
			}

			output.RenderBeginTag(HtmlTextWriterTag.Tr);
				output.WriteBeginTag("td");
				output.WriteAttribute("class", "Forum_Title1");
				output.WriteAttribute("colspan", "3");
				output.Write(HtmlTextWriter.TagRightChar);
					output.Write("&nbsp;");
				output.WriteEndTag("td");
			output.RenderEndTag();

			output.WriteEndTag("table");

			output.WriteEndTag("td");
			output.RenderEndTag();
			output.WriteEndTag("table");
		}
	}
}
